package com.platform.boot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.boot.converters.*;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.config.EnableR2dbcAuditing;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Alex bob(<a href="https://github.com/vnobo">https://github.com/vnobo</a>)
 */
@Configuration(proxyBeanMethods = false)
@EnableTransactionManagement
@EnableR2dbcAuditing
public class R2dbcConfiguration extends AbstractR2dbcConfiguration {

    protected final ObjectMapper objectMapper;

    public R2dbcConfiguration(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public ConnectionFactory connectionFactory() {
        return ConnectionFactories.get("r2dbc:..");
    }

    @Override
    public List<Object> getCustomConverters() {
        List<Object> converters = new ArrayList<>();
        converters.add(new SetReadConverter(this.objectMapper));
        converters.add(new SetWriteConverter(this.objectMapper));
        converters.add(new JsonNodeReadConverter(this.objectMapper));
        converters.add(new JsonNodeWriteConverter(this.objectMapper));
        converters.add(new UserAuditorReadConverter());
        converters.add(new UserAuditorWriteConverter());
        return converters;
    }
}